Newer
Older
taehui / qwilight-fe / src / app / [language] / note / query / useGetNote.ts
@Taehui Taehui on 6 Apr 1 KB 2024-04-07 오전 8:25
import { useNoteStore } from "@/state/Stores";
import { GetNoteAPI } from "@/type/wwwAPI";

import { wwwAPI } from "@/utilities/wwwAPI";
import { useQuery } from "@tanstack/react-query";
import { useParams } from "next/navigation";
import { useMemo } from "react";
import { useIntParam, useIsPath } from "taehui-ts/fe-utilities";

export default function useGetNote() {
  const { viewUnit } = useNoteStore();

  const { param: page } = useIntParam("page", 1);
  const { param: fit } = useIntParam("fit", 0);
  const { param: src } = useIntParam("src", 0);

  let { want: [want] = [""] } = useParams<{ want: string[] }>();
  want = useMemo(() => decodeURIComponent(want), [want]);

  const isPath = useIsPath();

  return useQuery({
    enabled: isPath("/note"),
    queryKey: ["note", fit, src, want, page, viewUnit],
    queryFn: async () => {
      const { data } = await wwwAPI.get<GetNoteAPI>("/note", {
        params: {
          fit,
          src,
          want,
          page,
          viewUnit,
        },
      });

      return data;
    },
    initialData: {
      totalCount: 0,
      topCount: 0,
      lastPage: 0,
      notes: [],
    },
  });
}